package org.frameworkset.tran.jobflow;
/**
 * Copyright 2025 bboss
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.frameworkset.tran.context.ImportContext;
import org.frameworkset.util.concurrent.IntegerCount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 顺序执行的复合流程节点
 * @author biaoping.yin
 * @Date 2025/3/31
 */
public class SequenceJobFlowNode extends CompositionJobFlowNode{
    private static Logger logger = LoggerFactory.getLogger(SequenceJobFlowNode.class);

    private JobFlowNode headerJobFlowNode;
    public SequenceJobFlowNode(){
        this.jobFlowNodeType = JobFlowNodeType.SEQUENCE;
    }

    public void setHeaderJobFlowNode(JobFlowNode headerJobFlowNode) {
        this.headerJobFlowNode = headerJobFlowNode;
        headerJobFlowNode.setCompositionJobFlowNode(this);
    }
    /**
     * 启动流程当前节点
     */
    @Override
    public boolean start(){
        
//        startNodes = new IntegerCount();
        if(assertTrigger()) {
            if (headerJobFlowNode == null) {
                throw new JobFlowException("SequenceJobFlowNode[id="+this.getNodeId()+",name="+this.getNodeName()+"]:headerJobFlowNode is null.");
            } else {
                logger.info("Start SequenceJobFlowNode[id={},name={}] begin.",this.getNodeId(),this.getNodeName());
//                for (int i = 0; i < jobFlowNodes.size(); i++) {
//                    JobFlowNode jobFlowNode = jobFlowNodes.get(i);
//                    if(jobFlowNode.start())
//                        startNodes.increament();
//                }                
                headerJobFlowNode.start();
                logger.info("Execute SequenceJobFlowNode[id={},name={}] complete.",this.getNodeId(),this.getNodeName());
//                this.nodeComplete( null);
            }
            return true;
        }
        else{
            logger.info("AssertTrigger: false,ignore execute this SequenceJobFlowNode[id={},name={}].",this.getNodeId(),this.getNodeName());
            nodeComplete(null);
        }
        return false;
        
    }

    /**
     * 停止流程当前节点
     */
    @Override
    public void stop() {

        try {
            for (int i = 0; jobFlowNodes != null && i < jobFlowNodes.size(); i++) {
                JobFlowNode jobFlowNode = jobFlowNodes.get(i);
                jobFlowNode.stop();
            }
        }
        catch (Exception e){
            logger.warn("Stop SequenceJobFlowNode[id="+this.getNodeId()+",name="+this.getNodeName()+"] failed:",e);
        }

        logger.info("Stop SequenceJobFlowNode[id="+this.getNodeId()+",name="+this.getNodeName()+"] complete.");
        if(this.nextJobFlowNode != null){
            try {
                this.nextJobFlowNode.stop();
//                logger.warn("Stop nextJobFlowNode of["+this.getNodeName()+"] complete.");
            }
            catch (Exception e){
                logger.warn("Stop nextJobFlowNode[id="+nextJobFlowNode.getNodeId()+",name="+nextJobFlowNode.getNodeName()+"] failed:",e);
            }
        }
    }

    /**
     * 串行分支完成
     * @param jobFlowNode
     */
    @Override
    public void brachComplete(JobFlowNode jobFlowNode, ImportContext importContext, Throwable e) {
//        int liveNodes = this.startNodes.decreament();
//        if(liveNodes <= 0){
//            this.nodeComplete(  importContext,   e);
//        }
        this.nodeComplete(importContext,e);
    }
}
